#include "UdpServer.hpp"
#include <memory>

string Handler(const string& s)
{
    string res = "Server handler message: ";
    res += s;

    return res;
}

string ExcuteCommand(const string& cmd)
{
    FILE* fp = popen(cmd.c_str(),"r");
    if(fp == nullptr)
    {
        perror("open fail");
        return "Error";
    }

    string res;
    char buffer[1024];
    while(true)
    {
        char* s = fgets(buffer,sizeof(buffer),fp);
        if(s == nullptr)
        {
            break;
        }

        res += s;
    }

    return res;
}

void Usage(string s)
{
    s += " port(>=1024)";
    cout<< s <<endl;
}

// ./udpserver port
int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 0;
    }

    uint16_t port = stoi(argv[1]);

    std::unique_ptr<UdpServer> Server(new UdpServer(port));

    Server->Init();
    // Server->Run(ExcuteCommand);
    Server->Run();


    return 0;
}